package yxy.game.pm2.active.command.turntable;

import yxy.cherry.data.bean.actturntablereward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_夺宝;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.Turntable;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActTurnTableDrawBoxRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Map;

@MessageType(proto = Command.ActTurnTableDrawBox, classtype = ActiveMessage.ActTurnTableDrawBoxRequest.class)
public class CMD_Active_夺宝_DrawBox extends Command<ActTurnTableDrawBoxRequest> {

	@Override
	public void execute(MessageSource messageSource, ActTurnTableDrawBoxRequest request) throws Exception {
		String playercode = messageSource.getTag();
		int type = request.getType();
		int boxid = request.getBoxid();
		ActiveMessage.ActTurnTableDrawBoxResponse.Builder response = ActiveMessage.ActTurnTableDrawBoxResponse.newBuilder();
		
		ActiveModule_夺宝 am = ActiveModule_夺宝.get(playercode,false);
		Turntable tt = am.gainturntable(type);
		
//		int luckvalue = tt.getLuckyvalue();
		Map<String, actturntablereward> ttrmap = ActDataCenter.getAllData(actturntablereward.class, playercode);
		actturntablereward ttr = ttrmap.get(String.valueOf(boxid));
//				DataCenter.getData(boxid, actturntablereward.class);
		if(ttr == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(tt.getLuckyboxs().contains(boxid)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
		
		CurrencyType curtype = null;
		if(type == ActiveModule_夺宝.普通夺宝) {
			curtype = CurrencyType.CT_普通夺宝积分;
		}
		if(type == ActiveModule_夺宝.高级夺宝) {
			curtype = CurrencyType.CT_高级夺宝积分;
		}
		
		if(Currency.number(playercode, curtype, -1,false) < ttr.getCondition()) {
			send(messageSource, response, new Result(Result.CostFail, curtype.getNumber()));
			return;
		}
		
		tt.getLuckyboxs().add(boxid);
		ArrayList<Integer> typeboxids = new ArrayList<Integer>();
		int typemax = 0;
		for(actturntablereward data : ttrmap.values()) {
			if(data.getType() == type) {
				typeboxids.add(data.getId());
				if(typemax == 0) {
					typemax = data.getCondition();
				}
				if(data.getCondition() >= typemax) {
					typemax = data.getCondition();
				}
			}
		}
		if(tt.getLuckyboxs().containsAll(typeboxids)) {
			Currency costcur = Currency.create(curtype, -1, typemax);
			new CurrencyHandle().cost(playercode, CCReason.活动_夺宝, costcur);
			tt.setLuckyboxs(new ArrayList<Integer>());
		}
		am.set(false);
		
		Currency cur = Currency.create(ttr.getRewardtype(), ttr.getRewardid(), ttr.getRewardnum());
		new CurrencyHandle().addCurrency(playercode, CCReason.活动_夺宝, cur);
		
		response.addDisplay(new CurrencyHandle().packItem(cur));
		response.addAllGetboxid(tt.getLuckyboxs());
		response.setType(type);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActTurnTableDrawBoxResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
